package cate.game.framework;

import javax.annotation.concurrent.NotThreadSafe;
import java.util.Objects;
import java.util.function.Supplier;

@NotThreadSafe
public class SingletonHolder<T> {
    private final Supplier<T> instanceSupplier;
    private T instance;

    public SingletonHolder(Supplier<T> instanceSupplier) {
        this.instanceSupplier = Objects.requireNonNull(instanceSupplier);
    }

    public T get() {
        if (Objects.isNull(instance)) {
            instance = instanceSupplier.get();
        }
        return instance;
    }
}
